import requests as req
from bs4 import BeautifulSoup

def getHomePage(url):
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
    params={'order_by':'points','spm':'smpc.fb-nba-home.top-dc.2.1620824904040nf6byr7'}
    res=req.get(url=url,params=params,headers=headers)
    res.encoding="gb2312"
    return res.text

def getUrlList(page):
    soup = BeautifulSoup(page,'lxml')
    urlList=soup.select('.e2')
    print(urlList)

def getPlayerPage(url):
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
    res=req.get(url=url,headers=headers)
    res.encoding="gb2312"
    return res.text

def getPlayerInfo(page):
    result={}
    soup = BeautifulSoup(page,'lxml')
    infoList['球员']=soup.select('.blockA').text
    infoList=soup.select('.pt>ul>li').text
    infoList['信息']=infoList
    return result


def main():
    homePage=getHomePage('http://data.sports.sohu.com/nba/nba_players_rank.php')
    urlList=getUrlList(homePage)
    playerFile=open('D://球员.txt','a',encoding="UTF-8")
    for i in urlList:
        playerPage=getPlayerPage(i)
        playerInfo=getPlayerInfo(playerPage)
        playerFile.write(playerInfo)
    
main()